/**
*
* Copyright (c) 2011~2012 galaxy engine team.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*
*  Abstract:
*    
*
*  Author:
*    freecnjet (freecnjet@gmail.com)
*
*  Revision History:
*    Created on: 2012-03-18
*
*/

#ifndef _OSGI_H_
#define _OSGI_H_


struct Module;
struct ServiceReference;
struct ModuleContext;


struct Version {
    int m_Major;
	int m_Minor;
	int m_Micro;
};


struct FrameworkEvent {
	enum {
		ERROR = 1,
		INFO,
		PACKAGES_REFRESHED,
		STARTED,
		STARTLEVEL_CHANGED,
		STOPPED,
		STOPPED_BOOTCLASSPATH_MODIFIED,
		STOPPED_UPDATE,
		WAIT_TIMEOUT,
		WARRING
	};

	int     m_Type;
	Module* m_Module;
};

struct FrameworkListener {
	virtual void FrameworkEvent(const FrameworkEvent* event) = 0;
};


struct ModuleEvent {
	enum {
		INSTALLED = 1,
		LAZY_ACTIVATION,
		RESOLVED,
		STARTED,
		STARTING,
		STOPPED,
		UNINSTALLED,
		UNRESOLVED,
		UPDATED
	};

	int     m_Type;
	Module* m_Module;
};

struct ModuleListener {
	virtual void ModuleChanged(const ModuleEvent* event) = 0;
};


struct ServiceEvent {
	enum {
		MODIFIED = 1,       //The properties of a registered service have been modified.
		MODIFIED_ENDMATCH,  //The properties of a registered service have been modified and the new properties no longer match the listener's filter.
		REGISTERED,         //This service has been registered.
		UNREGISTERING       //This service is in the process of being unregistered.
	};

	int               m_Type;
	ServiceReference* m_Reference;
};

struct ServiceListener {
	virtual void ServiceChanged(const ServiceEvent* event) = 0;
};


struct ServiceReference {
	virtual Module* GetModule() = 0;
	virtual void GetUsingModules(Module*& buf, size_t& count)  = 0;
};

struct ServiceRegistration {
    virtual ServiceReference* GetReference() = 0;
	virtual void Unregister() = 0;
};


struct Module
{
	enum {
		UNINSTALLED = 0,
		ACTIVE,                     //The bundle is now running.
		INSTALLED,                  //The bundle is installed but not yet resolved.
		RESOLVED,                   //The bundle is resolved and is able to be started.
		SIGNERS_ALL,                //Request that all certificates used to sign the bundle be returned.
		SIGNERS_TRUSTED,            //Request that only certificates used to sign the bundle that are trusted by the framework be returned.
		START_ACTIVATION_POLICY,    //The bundle start operation must activate the bundle according to the bundle's declared activation policy.
		START_TRANSIENT,            //The bundle start operation is transient and the persistent autostart setting of the bundle is not modified.
		STARTING,                   //The bundle is in the process of starting.
		STOP_TRANSIENT,             //The bundle stop is transient and the persistent autostart setting of the bundle is not modified.
		STOPPING,                   //The bundle is in the process of stopping.
	};

	virtual int GetBundleID() = 0;
	virtual ModuleContext* GetContext() = 0;
	virtual const char* GetSymbolicName() = 0;
	virtual const Version* GetVersion() = 0;
	virtual int GetState() = 0;
	virtual void Install() = 0;
	virtual void Start(int options) = 0;
	virtual void Stop(int options) = 0;
	virtual void Update() = 0;
	virtual void Uninstall() = 0;
};

struct ModuleContext {
	virtual void AddModuleListener( ModuleListener* ) = 0;
	virtual void RemoveModuleListener( ModuleListener* ) = 0;
	virtual void AddFrameworkListener( FrameworkListener* ) = 0;
	virtual void RemoveFrameworkListener( FrameworkListener* ) = 0;
	virtual void AddServiceListener( ServiceListener*, const char* filter ) = 0;
	virtual void RemoveServiceListener( ServiceListener* ) = 0;

	virtual Module* GetBoundle(long id) = 0;
	virtual void GetBoundles(Module*& buf, size_t& count ) = 0;
	virtual void* GetService( ServiceReference* ) = 0;
	virtual bool UngetService( ServiceReference* ) = 0;
	virtual ServiceReference* GetServiceReference( const char* clz, const char* filter ) = 0;
	virtual Module* InstallModule( const char* location ) = 0;
	virtual ServiceRegistration* RegisterService( const char* clz, void* service ) = 0;
};

struct Bootstrap : public Module
{
public:
	virtual void Init() = 0;
	virtual FrameworkEvent WaitForStop( long timeout ) = 0;
};


#endif
